/**
 * 服务接口
 * @author  Jello
 * @create  2018/11/02 09:45
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/

/**
 * 登录
 * @param {Object} param 参数
 * @param {Object} callback 回调 
 */
function login(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/login/login_api",
		data: "{\"phone\":\"" + param.phone + "\",\"password\":\"" + hex_md5(param.pwd) + "\",\"type\":\"" + 0 + "\"}",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: asktime,
		dataType: "json",
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			if(resultObj.code == 0) {
				callback(resultObj);
			} else {
				$("#error2").html(resultObj.msg);
				$("#error1").show();
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				$("#error2").html("请求超时");
				$("#error1").show();
			} else {
				$("#error2").html("请求数据失败");
				$("#error1").show();
			}
			return;
		},
	});
}

/**
 * 获取验证码接口,返回token
 * @param {Object} param
 * @param {Object} callback
 */
function gainCode(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/login/send_code?phone=" + param.phone + "&i=" + param.type + "&userToken=" + param.token,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: asktime,
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			callback(resultObj);
		}
	});
}

/**
 * 校验验证码接口
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function mateCode(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/login/mate_code?phone=" + param.phone + "&i=" + param.type + "&code=" + param.code + "&token=" + param.token,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var mateObj = eval(data);
			callback(mateObj);
		},
	});
}

/**
 * 注册
 * @param {Object} param
 * @param {Object} callback
 */
function regist(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/login/add_user",
		data: "{\"phone\":\"" + param.phone + "\",\"password\":\"" + hex_md5(param.pwd) + "\",\"type\":\"" + 0 + "\"}",
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: asktime,
		crossDomain: true,
		success: function(data) {
			var registObj = eval(data);
			if(registObj.code == 0 && registObj.data == 0) {
				callback(registObj.data);
			} else {
				$("#error4").html(registObj.msg);
				$("#error3").show();
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			if(status == 'timeout') {
				$("#error4").html("请求超时");
				$("#error3").show();
			} else {
				$("#error4").html("请求数据失败");
				$("#error3").show();
			}
			return;
		}
	});
}

/**
 * 获取个人资料
 * @param {Object} param 参数
 * @param {Object} callback 回调 
 */
function getUserMaterial(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userMaterial/select?userToken=" + param.token + "&status=1",
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var materialObj = eval(data);
			if(materialObj.code == 0 && materialObj.data != null) {
				callback(materialObj.data);
			} else if(materialObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				stopScroll();
				$(".errorbox").show();
				$(".errortext").html(materialObj.msg);
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			stopScroll();
			$(".errorbox").show();
			$(".errortext").html("网络异常");
		}
	});
}

/**
 * 查询理想岗位及曾任职工作
 * @param {Object} param
 * @param {Object} callback
 */
function queryMechanismList(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userMaterial/select_mechanism",
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				callback(resultObj.data);
			} else {
				stopScroll();
				$(".errorbox").show();
				$(".errortext").html(resultObj.msg);
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			stopScroll();
			$(".errorbox").show();
			$(".errortext").html("网络异常");
		}
	});
}

/**
 * 退出
 */
function loginOut() {
	$.ajax({
		type: "get",
		url: serverUrl + "/login/user_sign_out",
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0) {
				sessionStorage.clear();
				window.location.href = "login.html";
			}
		},
	});
}

/**
 * 取消报名
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function cancelApply(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/work/cancleApply?userToken=" + param.token + "&workId=" + param.workId + "&status=1",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			closeSpin(300);
			stopScroll();
			$(".wran_box").show();
			$(".warntext").html("网络异常");
			return;
		}
	});
}

/**
 * 入职报名
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function entryApply(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/work/entryApply?userToken=" + param.token + "&type=" + param.type + "&status=1",
		data: "{\"reTime\":\"" + param.reTime + "\",\"reMoney\":\"" + param.reMoney + "\",\"identity\":\"" + param.identy + "\",\"interviewTime\":\"" + param.interviewTime + "\",\"mechanismId\":\"" + param.mechanismId + "\",\"mechanismName\":\"" + param.mechanismName + "\",\"userName\":\"" + param.userName + "\",\"workId\":\"" + param.workId + "\",\"recruitAddress\":\"" + param.recruitAddress + "\"}",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			$(".applybox").hide();
			$(".wran_box").show();
			return;
		}
	});
}

/**
 * 查询招聘详情
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function queryWorkDetail(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_workdetail?workId=" + param.workId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				callback(resultObj);
			} else {
				closeSpin(300);
				stopScroll();
				$(".errorbox").show();
				$(".errortext").html(resultObj.msg);
				return;
			}

		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			stopScroll();
			$(".errorbox").show();
			$(".errortext").html("网络异常");
			return;
		}
	});
}

/**
 * 查询是否报名、收藏
 */
function queryIsApplyOrCollect(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_isApplyOrIsCollection?userToken=" + param.token + "&status=" + param.login_status + "&workId=" + param.workId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0) {
				callback(resultObj.data);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				stopScroll();
				$(".errorbox").show();
				$(".errortext").html(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			stopScroll();
			$(".errorbox").show();
			$(".errortext").html("网络异常");
			return;
		}
	});
}

/**
 * 查询所有行业及工种
 * @param {Object} callback
 */
function queryIndustryList(callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_mechanismlist",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: asktime,
		dataType: "json",
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0) {
				callback(resultObj.data);
			}
		}
	});
}

/**
 * 收藏/取消收藏
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function collectJob(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/social/set_collection?userToken=" + param.token + "&type=" + param.type + "&status=1" + "&id=" + param.id,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			stopScroll();
			$(".errorbox").show();
			$(".errortext").html("网络异常");
			return;
		}
	});
}

/**
 * 忘记密码
 */
function forgetPwd(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/login/set_psw",
		data: param.data,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: asktime,
		crossDomain: true,
		success: function(data) {
			var forgetObj = eval(data);
			callback(forgetObj);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$(".wran_box").show();
			$(".warntext").html("网络异常");
			return;
		}
	});
}